#! /usr/bin/env python
# -*- coding: utf-8 -*-

import cProxy
import re
import requests
import threading
import time


def get_current_ip(html):
    pattern = re.compile(r'<p>您现在的 IP：<code>(\d.*?)</code></p>')
    ip = re.findall(pattern, html)
    return ip


def test_proxy(proxy):
    p = {'http': 'http://'+proxy, 'https': 'https://'+proxy}
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)"}

    try:
        r = requests.get('https://ip.cn/', headers=headers, proxies=p, timeout=3)
        if (r.status_code == 200):
            # r.encoding = 'gbk'
            print(get_current_ip(r.text), proxy)
        else:
            print('Failed: ' + str(r.status_code))
    except:
        print('Time out')
        pass


def check_proxy(proxies):
    cpcp = cProxy.CollectProxy()
    for proxy in proxies:

        if cpcp.check(proxy, 'https://blog.csdn.net'):
            print('Checking ' + str(proxy) + '\t: Done')
            proxy_list.append(proxy)
        else:
            print('Checking ' + str(proxy) + '\t: Failed')
    return proxy_list


proxy_list = []


def main():
    '''Find available proxies with multi-threads'''
    thread_cnt = 50
    threads = []
    cpcp = cProxy.CollectProxy()
    raw_proxy = cpcp.do(1)

    proxies = list(raw_proxy)
    l = len(proxies)
    l += thread_cnt - 1

    start_time = time.time()
    for i in range(thread_cnt):
        start = l // thread_cnt * i
        end = l // thread_cnt * (i + 1)
        t = threading.Thread(target=check_proxy, args=[proxies[start:end]])
        threads.append(t)

    for i in range(len(threads)):
        threads[i].start()

    for i in range(len(threads)):
        threads[i].join()

    print(proxy_list)
    end_time = time.time()

    print(str(end_time - start_time) + ' s')

    # cppp = cProxy.ProxyPool()
    # proxy = cppp.getProxy('https://blog.csdn.net')
    # print(proxy)

    # test_proxy(proxy)


if __name__ == '__main__':
    main()
